package org.example;

import java.util.Scanner;

public class test3 {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        //求1~100000之间的质数个数
        int count= 0;
        int sum = 0;
        for (int i = 2; i <=100000; i++) {
            if (isPrime(i)) {
                count++;
                sum += i;
                System.out.println(i);
            }
        }
        System.out.println("1~100000之间的质数个数为"+count);
        System.out.println("1~100000之间的质数和为"+sum);
        //获取当前时间
        long end = System.currentTimeMillis();
        //获取程序运行时间
        System.out.println("程序运行时间为"+(end-start)+"ms");
    }
    public static boolean isPrime(int number) {
        boolean flag = true;
        for (int i = 2; i <= Math.sqrt(number); i++) {
            //用平方根来优化循环次数，减少循环次数
            if (number % i == 0) {
                flag = false;
                break;
            }
        }
        /*if (flag) {
            System.out.println("这个整数是质数");
        } else {
            System.out.println("这个整数不是质数");
        }*/
        return flag;
    }
}
